package yzx.firstletter.appList;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import yzx.common.view.util.BaseRecyclerViewAdapter;
import yzx.firstletter.R;
import yzx.firstletter.util.AppInstallUtil;
import yzx.firstletter.util.GenericCallback;

public class AppListAdapter extends BaseRecyclerViewAdapter{

    private List<AppInstallUtil.AppInfo> list;
    public AppListAdapter(List<AppInstallUtil.AppInfo> list){
        this.list = list;
        if(this.list==null)
            this.list = new ArrayList<>(0);
    }
    public List<AppInstallUtil.AppInfo> getList() {
        return list;
    }


    // item click callback
    public GenericCallback<AppInstallUtil.AppInfo> onItemClickListener;

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_app_list, parent, false);
        return new BaseViewHolder(view);
    }
    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        AppInstallUtil.AppInfo info = list.get(position);

        ImageView iconView = (ImageView) holder.getView(R.id.iv_icon);
        iconView.setImageDrawable(AppInstallUtil.getIconByPackage(iconView.getContext(), info.packageName));

        TextView nameView = (TextView) holder.getView(R.id.tv_name);
        nameView.setText(info.name);

        holder.getView(R.id.container).setTag(info);
        holder.getView(R.id.container).setOnClickListener(onClickListener);
    }
    @Override
    public int getItemCount() {
        return list.size();
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        public void onClick(View v) {
            if(onItemClickListener != null)
                onItemClickListener.callback((AppInstallUtil.AppInfo) v.getTag());
        }
    };

}
